/*
 * Created by JFormDesigner on Wed Sep 08 19:13:10 CST 2021
 */

package com.minelx.woods.pc.form;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.minelx.httpclient.core.request.IResponse;
import com.minelx.util.IOUtil;
import com.minelx.woods.pc.service.TaskService;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author aaa
 */
public class TaskDetailDialog extends JDialog {

	public static final int INIT = 0;

	public static final int READY = 1;

	public static final int RUNNING = 2;

	private final JSONObject taskAsJSON;

	private final TaskService taskService;

	public TaskDetailDialog(Window owner, TaskService taskService, JSONObject taskAsJSON) {
		super(owner, "", ModalityType.APPLICATION_MODAL);
		initComponents();

		this.taskAsJSON = taskAsJSON;
		this.taskService = taskService;

		// preparing content
		refreshContent(taskAsJSON);
	}

	private void viewCollectionsButtonActionPerformed(ActionEvent e) {
		new ViewCollectionsDialog(this, taskAsJSON.getJSONObject("info").getJSONArray("collections")).setVisible(true);
	}

	private void viewVariantCollectionsButtonActionPerformed(ActionEvent e) {
		new ViewVariantCollectionsDialog(this, taskAsJSON.getJSONObject("info").getJSONArray("variantCollections")).setVisible(true);
	}

	private void downloadButtonActionPerformed(ActionEvent e) {
		// input the destination file path
		JFileChooser chooser = new JFileChooser(System.getProperty("user.dir"));
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		int option = chooser.showSaveDialog(this);
		if (option != JFileChooser.APPROVE_OPTION) {
			throw new IllegalStateException("user canceled.");
		}
		String absolutePath = chooser.getSelectedFile().getAbsolutePath();

		IResponse response = taskService.postDownloadRequest(taskAsJSON.getString("id"));
		JSONObject responseAsJSON = JSON.parseObject(response.contentAsText(StandardCharsets.UTF_8));
		String requestId = responseAsJSON.getString("requestId");

		IResponse fileContentResponse = taskService.downloadTask(requestId);
		ByteArrayInputStream fileInputStream = new ByteArrayInputStream(fileContentResponse.content());
		try (FileOutputStream output = new FileOutputStream(absolutePath)) {
			IOUtil.exchange(fileInputStream, output);
		} catch (IOException ex) {
			throw new RuntimeException("file " + absolutePath + " is not available.", ex);
		}
	}

	private void refreshContent(JSONObject taskAsJSON) {
		sourceTypeText.setText(taskAsJSON.getJSONObject("info").getString("sourceType"));
		sourceVariablesText.setText(taskAsJSON.getJSONObject("info").getString("sourceVariables"));
		// set hint by task condition
		int condition = taskAsJSON.getIntValue("condition");
		if (condition == INIT) {
			taskConditionText.setText("未准备执行");
		} else if (condition == READY) {
			taskConditionText.setText("等待执行");
		} else if (condition == RUNNING) {
			taskConditionText.setText("正在执行");
		} else {
			throw new RuntimeException("found an unknown condition: " + condition);
		}
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		panel3 = new JPanel();
		panel2 = new JPanel();
		label4 = new JLabel();
		sourceTypeText = new JTextField();
		label5 = new JLabel();
		sourceVariablesText = new JTextField();
		label6 = new JLabel();
		viewCollectionsButton = new JButton();
		label7 = new JLabel();
		viewVariantCollectionsButton = new JButton();
		label8 = new JLabel();
		taskConditionText = new JTextField();
		label2 = new JLabel();
		panel1 = new JPanel();
		downloadButton = new JButton();

		//======== this ========
		setTitle("\u5ba1\u6838\u8be6\u60c5");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		var contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== dialogPane ========
		{
			dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
			dialogPane.setLayout(new BorderLayout());

			//======== contentPanel ========
			{
				contentPanel.setLayout(new BorderLayout());

				//======== panel3 ========
				{
					panel3.setLayout(new BorderLayout());

					//======== panel2 ========
					{
						panel2.setLayout(new GridLayoutManager(6, 2, new Insets(0, 0, 0, 0), 0, 0));

						//---- label4 ----
						label4.setText("\u8fc7\u6ee4\u65b9\u5f0f: ");
						label4.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
						panel2.add(label4, new GridConstraints(0, 0, 1, 1,
							GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							null, null, null));

						//---- sourceTypeText ----
						sourceTypeText.setEditable(false);
						sourceTypeText.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 26));
						panel2.add(sourceTypeText, new GridConstraints(0, 1, 1, 1,
							GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							new Dimension(300, 0), null, null));

						//---- label5 ----
						label5.setText("\u8fc7\u6ee4\u53c2\u6570: ");
						label5.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
						panel2.add(label5, new GridConstraints(1, 0, 1, 1,
							GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							null, null, null));

						//---- sourceVariablesText ----
						sourceVariablesText.setEditable(false);
						sourceVariablesText.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 26));
						panel2.add(sourceVariablesText, new GridConstraints(1, 1, 1, 1,
							GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							new Dimension(300, 0), null, null));

						//---- label6 ----
						label6.setText("\u5b57\u6bb5: ");
						label6.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
						panel2.add(label6, new GridConstraints(2, 0, 1, 1,
							GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							null, null, null));

						//---- viewCollectionsButton ----
						viewCollectionsButton.setText("\u67e5\u770b\u8be5\u4efb\u52a1\u5b57\u6bb5");
						viewCollectionsButton.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
						viewCollectionsButton.addActionListener(e -> viewCollectionsButtonActionPerformed(e));
						panel2.add(viewCollectionsButton, new GridConstraints(2, 1, 1, 1,
							GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							null, null, null));

						//---- label7 ----
						label7.setText("\u53d8\u91cf\u5b57\u6bb5: ");
						label7.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
						panel2.add(label7, new GridConstraints(3, 0, 1, 1,
							GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							null, null, null));

						//---- viewVariantCollectionsButton ----
						viewVariantCollectionsButton.setText("\u67e5\u770b\u8be5\u4efb\u52a1\u7684\u53d8\u91cf\u5b57\u6bb5");
						viewVariantCollectionsButton.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
						viewVariantCollectionsButton.addActionListener(e -> viewVariantCollectionsButtonActionPerformed(e));
						panel2.add(viewVariantCollectionsButton, new GridConstraints(3, 1, 1, 1,
							GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							null, null, null));

						//---- label8 ----
						label8.setText("\u4efb\u52a1\u72b6\u6001: ");
						label8.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
						panel2.add(label8, new GridConstraints(4, 0, 1, 1,
							GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							null, null, null));

						//---- taskConditionText ----
						taskConditionText.setEditable(false);
						taskConditionText.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 26));
						panel2.add(taskConditionText, new GridConstraints(4, 1, 1, 1,
							GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
							new Dimension(300, 0), null, null));
					}
					panel3.add(panel2, BorderLayout.CENTER);

					//---- label2 ----
					label2.setText("\u4efb\u52a1\u8be6\u60c5");
					label2.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 35));
					label2.setHorizontalAlignment(SwingConstants.CENTER);
					panel3.add(label2, BorderLayout.NORTH);
				}
				contentPanel.add(panel3, BorderLayout.CENTER);
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);

			//======== panel1 ========
			{
				panel1.setLayout(new FlowLayout(FlowLayout.TRAILING));

				//---- downloadButton ----
				downloadButton.setText("\u5c1d\u8bd5\u4e0b\u8f7d");
				downloadButton.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
				downloadButton.addActionListener(e -> downloadButtonActionPerformed(e));
				panel1.add(downloadButton);
			}
			dialogPane.add(panel1, BorderLayout.SOUTH);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		setSize(505, 605);
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JPanel panel3;
	private JPanel panel2;
	private JLabel label4;
	private JTextField sourceTypeText;
	private JLabel label5;
	private JTextField sourceVariablesText;
	private JLabel label6;
	private JButton viewCollectionsButton;
	private JLabel label7;
	private JButton viewVariantCollectionsButton;
	private JLabel label8;
	private JTextField taskConditionText;
	private JLabel label2;
	private JPanel panel1;
	private JButton downloadButton;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
